package calendar.view.forms;

import com.google.api.client.util.DateTime;
import com.google.api.services.calendar.model.Event;
import com.google.api.services.calendar.model.EventDateTime;
import swing.FormPanel;
import swing.form.fields.DateField;

import javax.swing.*;
import java.util.Date;
import java.util.TimeZone;

public class EventForm extends FormPanel<Event> {
    private static final int SECOND = 1;
    private static final int MINUTE = 60 * SECOND;
    private static final int HOUR = 60 * MINUTE;
    private static final int DAY = 24 * HOUR;

    public EventForm() {
        super("Add");

        final Date startDate = new Date();

        final JTextField summaryField = addField("Summary", new JTextField("New Event"));
        final DateField startDateField = addField("Start date", new DateField(startDate));
        final DateField endDateField = addField("End date", new DateField(new Date(startDate.getTime() + 3 * DAY)));

        defaultButton.addActionListener(e -> {
            Event event = new Event();
            event.setSummary(summaryField.getText());
            DateTime start = new DateTime((Date) startDateField.getValue(), TimeZone.getTimeZone("UTC"));
            event.setStart(new EventDateTime().setDateTime(start));
            DateTime end = new DateTime((Date) endDateField.getValue(), TimeZone.getTimeZone("UTC"));
            event.setEnd(new EventDateTime().setDateTime(end));

            setItem(event);

            /*new CreateEvent(client)
                    .setEvent(event)
                    .execute();*/
        });
    }

}
